package com.mercenary.wein.screenControl.ui.util;

import com.mercenary.wein.screenControl.bean.CommandMetaBean;
import com.mercenary.wein.screenControl.dao.ConnectDb;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by tuxiao on 2017/3/20.
 */
public class Constants {
    public static String DeviceFilePath ;
    public static String ADBFilePath ;
    public static String RepertoryPath;
    public static String LocalTempFilePath;
    public static Map<Long, String> actionMap = new HashMap<Long, String>();
    public static Log log = LogFactory.getLog(Constants.class);

    static {
        String path = System.getProperty("user.dir");
        log.info("当前目录："+path);
        File prop = new File(path+"\\conf\\sysconf.properties");
        if(!prop.exists()){
            log.info("未找到配置文件，使用系统默认配置");
            DeviceFilePath="/storage/sdcard0/Download/";
            ADBFilePath=path+"\\src\\main\\ADB\\adb.exe";
            RepertoryPath=path+"\\src\\main\\resources";
            LocalTempFilePath=path+"\\src\\main\\temp\\";
        }else{
            log.info("初始化配置");
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream(prop));
                DeviceFilePath=properties.getProperty("DeviceFilePath");
                ADBFilePath=properties.getProperty("ADBFilePath");
                RepertoryPath=properties.getProperty("RepertoryPath");
                LocalTempFilePath=properties.getProperty("LocalTempFilePath");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        log.info("加载ADB文件："+Constants.ADBFilePath) ;
        log.info("设备存放文件路径为："+Constants.DeviceFilePath) ;
        log.info("文件选择默认位置为："+Constants.RepertoryPath) ;

        String sql = "select * from commandmeta";
        Connection connection = ConnectDb.Connect();
        QueryRunner queryRunner = new QueryRunner();
        try {
            List<CommandMetaBean> results = queryRunner.query(connection, sql,
                    new BeanListHandler<CommandMetaBean>(CommandMetaBean.class));
            for(CommandMetaBean c:results){
                actionMap.put(c.getActionId(),c.getActionName());
            }
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
